package com.hfun.server.service${packageName}.impl;

import com.hfun.common.utils.JsonUtil;
import com.hfun.server.domain${packageName}.${objectName}Model;
import com.hfun.domain.request.HttpDataParam;
import com.hfun.domain.response.ResponseData;
import com.hfun.server.common.IConstants;
import com.hfun.server.dao.MapperFactory;
import com.hfun.server.service${packageName}.${objectName}Service;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;

/**
* ${description}
* Created by AUTO on ${nowDate?string("yyyy/MM/dd")}.
*/
@Service
public class ${objectName}ServiceImpl implements ${objectName}Service{
    @Resource
    private MapperFactory mapperFactory;

    /**
     * 新增
     * @param dataCondition
     * @return
     * @throws RuntimeException
     */
    @Override
    public String insert(HttpDataParam<${objectName}Model> dataCondition) throws RuntimeException{
        List<${objectName}Model> list = dataCondition.getObjects();
        ResponseData responseData = new ResponseData();
        Date date = new Date();
        if(null != list){
            for (${objectName}Model model : list) {
                try {
                    mapperFactory.getw${objectName}Mapper().insert(model);
                } catch (Exception e) {
                    throw new RuntimeException(e.toString());
                }
            }
            responseData.setData(null);
        }else{
            responseData.setCode(IConstants.ResponseDefine.Code.ERROR_402.getIndex());
            responseData.setMsg("参数为空");
        }

        return JsonUtil.toJSONString(responseData, false);
    }

    /**
     * 修改
     * @param dataCondition
     * @return
     * @throws RuntimeException
     */
    @Override
    public String update(HttpDataParam<${objectName}Model> dataCondition) throws RuntimeException{
        List<${objectName}Model> list = dataCondition.getObjects();
        ResponseData responseData = new ResponseData();
        if(null != list){
            for (${objectName}Model model : list) {
                try {
                    mapperFactory.getw${objectName}Mapper().updateByPrimaryKeySelective(model);
                } catch (Exception e) {
                    throw new RuntimeException(e.toString());
                }
            }
            responseData.setData(null);
        }else{
            responseData.setCode(IConstants.ResponseDefine.Code.ERROR_402.getIndex());
            responseData.setMsg("参数为空");
        }

        return JsonUtil.toJSONString(responseData, false);
    }

    /**
     * 查询
     * @param model
     * @return
     */
    @Override
    public String queryList(${objectName}Model model) {
        ResponseData responseData = new ResponseData();
        //是否需要返回totalCount
        if(model.needTotalCount()){
            responseData.setTotalCount(mapperFactory.getr${objectName}Mapper().selectCountByObject(model));
        }
        List<${objectName}Model> list = mapperFactory.getr${objectName}Mapper().selectByObejctPaging(model);

        if (list.isEmpty()) {
            responseData.setCode(IConstants.ResponseDefine.Code.SUCCESS_200.getIndex());
            responseData.setMsg("没有查询到数据");
            responseData.setData(new ArrayList<${objectName}Model>());
        } else {
            responseData.setData(list);
        }

        return JsonUtil.toJSONString(responseData, false);
    }

    /**
     * 删除
     * @param dataCondition
     * @return
     */
    @Override
    public String delete(HttpDataParam<${objectName}Model> dataCondition) throws RuntimeException{
        List<${objectName}Model> list = dataCondition.getObjects();
        ResponseData responseData = new ResponseData();
        if(null != list){
            for (${objectName}Model model : list) {
                try {
                    mapperFactory.getw${objectName}Mapper().deleteByPrimaryKey(model.getId());
                } catch (Exception e) {
                    throw new RuntimeException(e.toString());
                }
            }
            responseData.setData(null);
        }else{
            responseData.setCode(IConstants.ResponseDefine.Code.ERROR_402.getIndex());
            responseData.setMsg("参数为空");
        }

        return JsonUtil.toJSONString(responseData, false);
    }
}
